﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;
using Model.Data;

namespace WssClient.Item.ZhongFu
{
    public class SalesStockOut : BaseItemData<SalesStockOut>
    {
        /// <summary>
        /// 日期
        /// </summary>
        public string date { get; set; }
        /// <summary>
        /// 单号
        /// </summary>
        public string order { get; set; }
        /// <summary>
        /// 客户名称
        /// </summary>
        public string customer { get; set; }
        /// <summary>
        /// 产品列表
        /// </summary>
        public List<Product> products { get; set; } = new List<Product>();
        /// <summary>
        /// 合计件数
        /// </summary>
        public int packagesTotal { get; set; } = 0;
        /// <summary>
        /// 合计数量
        /// </summary>
        public double numberTotal { get; set; } = 0.00;
        /// <summary>
        /// 合计金额
        /// </summary>
        public double amountToal { get; set; } = 0.00;
        /// <summary>
        /// 大写金额
        /// </summary>
        public string upperCaseAmount { get; set; }
        /// <summary>
        /// 上期欠款
        /// </summary>
        public double lastIssueArrears { get; set; }
        /// <summary>
        /// 累计欠款
        /// </summary>
        public double addUpArrears { get; set; }
        /// <summary>
        /// 开单人
        /// </summary>
        public string billingPerson { get; set; }

        public override void Analysis(RequestData requestData)
        {
            try
            {
                dynamic data = requestData.data;

                string _date = data.date;
                
                date = _date.Split(' ')[0];
                order = data.order;
                customer = data.customer;

                string packagesTotalStr = data.packagesTotal;
                packagesTotal = !string.IsNullOrEmpty(packagesTotalStr) ? int.Parse(packagesTotalStr) : 0;

                string numberTotalStr = data.numberTotal;
                numberTotal = !string.IsNullOrEmpty(numberTotalStr) ? double.Parse(numberTotalStr) : 0;

                string amountTotalStr = data.amountToal;
                amountToal = !string.IsNullOrEmpty(amountTotalStr) ? double.Parse(amountTotalStr) : 0;

                upperCaseAmount = data.upperCaseAmount;

                string lastIssueArrearsStr = data.lastIssueArrears;
                lastIssueArrears = !string.IsNullOrEmpty(lastIssueArrearsStr) ? double.Parse(lastIssueArrearsStr) : 0;

                string addUpArrearsStr = data.addUpArrears;
                addUpArrears = !string.IsNullOrEmpty(addUpArrearsStr) ? double.Parse(addUpArrearsStr) : 0;

                Dictionary<string, object> dataDic = DynamicHelper.toDic(data);
                dynamic operatorObj = CommonHelper.getValueByDic(dataDic, "operator");
                billingPerson = operatorObj.name;

                int idx = 1;

                JArray generals = data.general;
                foreach (dynamic item in generals)
                {
                    Product product = Product.Analysis(item, true);
                    product.idx = idx++;

                    products.Add(product);
                }

                JArray pmzmxs = data.pmzmx;
                foreach (dynamic item in pmzmxs)
                {
                    Product product = Product.Analysis(item, false);
                    product.idx = idx++;

                    products.Add(product);
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
